package com.Akronmcp.hta.obj;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;

import com.Akronmcp.hta.main.HM;
import com.gmail.filoghost.holographicdisplays.api.Hologram;
import com.gmail.filoghost.holographicdisplays.api.HologramsAPI;
import com.gmail.filoghost.holographicdisplays.api.handler.TouchHandler;
import com.gmail.filoghost.holographicdisplays.api.line.HologramLine;
import com.gmail.filoghost.holographicdisplays.api.line.TextLine;

public class HopHolo extends HPar2
{
		
	  Location location = null;
	  //地址
	  
	  List<String> preList = new ArrayList<>();
	  List<String> sufList = new ArrayList<>();
	  //前后缀
	  Hologram holo;
	 
	  TouchHandler th;
	  
	  List<List<String>> fps = new ArrayList<>();
	  
	  
	public void create()
	{
		delete();
		
		holo = HologramsAPI.createHologram(HM.ins,location);
		holo.getVisibilityManager().setVisibleByDefault(true);
		holo.setAllowPlaceholders(true);
		
		for(String str: preList)
		{
			holo.appendTextLine(str).setTouchHandler(th);
		}
		//设置前缀
		
		for(String str: sufList)
		{
			holo.appendTextLine(str).setTouchHandler(th);
		}
		//设置后缀
		send();
	}
	
	public void delete()
	{
		if(thr!=null)
			thr.cancel();
		if(holo!=null)
		holo.delete();
	}
	  
	public List<String> getPreList() {
		return preList;
	}
	
	public void setPreList(List<String> preList) {
		this.preList = preList;
	}
	
	public List<String> getSufList() {
		return sufList;
	}
	
	public void setSufList(List<String> sufList) {
		this.sufList = sufList;
	}
	  
	public Location getLocation() {
		return location;
	}

	@Nonnull
	public void setLocation(Location location) {
		this.location = location;
		
	}
	
	public HopHolo(HManager hta) {
		super(hta);
	}
	
	public void setTouchCmd(String cmd)
	{
		th = (p)->
		{
			if(cmd.startsWith("console:"))
			{
				Bukkit.dispatchCommand(Bukkit.getConsoleSender(),cmd.substring(8));
			}else
			p.performCommand(cmd);
		};
	}
	
	
	public Hologram getHolo() {
		return holo;
	}

	public void setHolo(Hologram holo) {
		this.holo = holo;
	}

	public BukkitTask getThr() {
		return thr;
	}

	public void setThr(BukkitTask thr) {
		this.thr = thr;
	}

	public List<List<String>> getFps() {
		return fps;
	}

	public void setFps(List<List<String>> fps) {
		this.fps = fps;
	}

	public void send()
	{
		send(new ArrayList<>());
	}
	
	BukkitTask thr;
	
	/**参数1已无意义*/
	public void send(List<Player> pl) {
		
		 if(thr!=null)
			 thr.cancel();
		 
		 
		if(fps.isEmpty())
		{
			if(module!=null)
			if(!iscopyedmodule||module.equals("auto"))
				this.setModule(module); 
			
			TextLine tl = holo.insertTextLine(preList.size(),text);
			
			 tl.setTouchHandler(th);
			 thr = new BukkitRunnable() {
					
	            int index = 0;
	            //下标
	            
	            public void run() 
	            {//重写run方法
	            	
					if((!sl.isEmpty()))
					{//开始闪烁,遍历集合
						String str = sl.get(index);
						           	      
						if(str!=null)                	 
						if(!str.equals("-"))
					    {
							tl.setText(str);
					    }
						
					}else cancel();
	            	
	            	if(index+1 < sl.size())
	            		index++;
	            	else
	            		index = 0;
	            } 
	        }.runTaskTimerAsynchronously(HM.ins, 0L, ticktime);
		}else
		 thr = new BukkitRunnable() {
			
            int index = 0;
            //下标
            {
            	holo.clearLines();
            }
            
            public void run() 
            {//重写run方法
            	
            	if(!fps.isEmpty())
            	{
            		List<String> tlist = fps.get(index);
            		
            		if(tlist.size()!=holo.size())
            		{
            			synchronized (pl) {
							holo.clearLines();
							for(String o:tlist)
							holo.appendTextLine(o).setTouchHandler(th);
						}
            		}else
            		{
            			int i=0;
            			for(String o:tlist)
            			{
            				if(i<holo.size())
            				{
            					HologramLine hl = holo.getLine(i);
            					if(hl instanceof TextLine)
            					((TextLine) hl).setText(o);
            				}
            				i++;
            			}
            		}
            	}else 
            	{
            		cancel();
            	}
            	
            	if(index+1 < fps.size())
            		index++;
            	else
            		index = 0;
            } 
        }.runTaskTimer(HM.ins, 0L, ticktime);	   
	}

	public void addToList(String id) {
		this.id = id;
		hta.addHolo(this);
	}
	
	
}
